<template>
  <div class="content-box">
    <TreeFilter
      :default-value="treeFilterValue.departmentId"
      @change="changeTreeFilter"
    />

    <div class="descriptions-box card">
      <div>&nbsp;</div>
      <BaseList
        :loading="loading"
        :data="dataList"
        :total="total"
        @reflush="fetchFormsList"
      ></BaseList>
    </div>
  </div>
</template>

<script setup lang="ts" name="treeFilter">
import { ref, reactive, computed } from "vue";
import { useStore } from 'vuex'
import TreeFilter from "./components/TreeFilter.vue";
import BaseList from './components/BaseList.vue'
import { queryOrg, queryDepartment, queryClassStudent, queryTeachers } from '@/management/api/basedata'
import { ORGTREE_TYPE } from '@/management/utils/constant.ts'

const store = useStore()

const treeFilterValue = reactive({ departmentId: "-1" });
const loading = ref(false)
const total = ref(0)
const dataList = ref([])
const formsList = computed(() => {
  return store.state.dataform.formsList
})
const fetchFormsList = async (params?: any) => {
  if (!params) {
    params = {
      pageSize: 10,
      curPage: 1
    }
  }
  loading.value = true
  await store.dispatch('dataform/getFormsList', params)
  loading.value = false
}
const changeTreeFilter = (val: any) => {
  console.log(val);
  // ElMessage.success();
  getDetail(val)
  treeFilterValue.departmentId = val.id;
};
const getDetail = (row: any) => {
  switch (row?.type) {
    case ORGTREE_TYPE.REGION:
      getOrgDetail(row)
      getTeachers(row)
      return 
    case ORGTREE_TYPE.REGION_EDU:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.SCHOOL:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.SCHOOL_EDU:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.PROVINCE_EDU:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.GROUP_SCHOOL:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.OFFICE_ORG:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.OFFICE_ORG_NODE:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.TEACH_ORG:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.SECTION:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.ORG_CLASS:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.TEACH_CLASS:
      getOrgDetail(row)
      getDeptDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.GRADE:
      getOrgDetail(row)
      getTeachers(row)
      return
    case ORGTREE_TYPE.CLASS:
      getOrgDetail(row)
      getClassStudent(row)
      return
    default:
      getClassStudent(row)
      return 
  }
}
const getOrgDetail = async (val: any) => {
  const res = await queryOrg(val ? val.id : (treeFilterValue.departmentId === '-1' ? (window as any).$app.user.orgId : treeFilterValue.departmentId))
  console.log(res);
}
const getDeptDetail = async (val: any) => {
  const res = await queryDepartment(val ? val.id : (treeFilterValue.departmentId === '-1' ? (window as any).$app.user.orgId : treeFilterValue.departmentId))
  console.log(res);
}

const getClassStudent = async (val: any) => {
  const res: any = await queryClassStudent(val ? val.id : (treeFilterValue.departmentId === '-1' ? (window as any).$app.user.orgId : treeFilterValue.departmentId))
  console.log(res);
  dataList.value = (res?.pageInfo?.list || []).map((item: any) => {
    return {
      ...item,
      formName: item.name,
      formId: item.userId,
      type: item.type
    }
  })
  total.value = res?.pageInfo?.total || 0
}

const getTeachers = async (val: any) => {
  const res: any = await queryTeachers({ 
    orgIdList: [val ? val.id : (treeFilterValue.departmentId === '-1' ? (window as any).$app.user.orgId : treeFilterValue.departmentId)]
  })
  console.log(res);
  dataList.value = (res?.pageInfo?.list || []).map((item: any) => {
    return {
      ...item,
      formName: item.name,
      formId: item.userId,
      type: item.type
    }
  })
  total.value = res?.pageInfo?.total || 0
}
</script>

<style scoped lang="scss">
@use "./index.scss";
</style>
